/****************************************************************
 *Author:       zxx
 *UnityVersion：2019.4.1f1
 *Date:         2020/07/30 09:56:01
 *Description:    
*****************************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using DG.Tweening.Plugins.Core.PathCore;

public class EnemyController : MonoBehaviour {

    
    private float time;

    private Animator animator;

    private AudioSource source;

    [Range(1,10)]
    public float Xoffset = 5;

    [Range(1, 5)]
    public float speed = 3;

    public bool isMove = true;

    private Vector3 fromPos;
    private Vector3 toPos;

    private void Awake()
    {
        animator = GetComponent<Animator>();
        source = GetComponent<AudioSource>();
        fromPos = new Vector3(transform.position.x - Xoffset, transform.position.y, transform.position.z);
        toPos = new Vector3(transform.position.x + Xoffset, transform.position.y, transform.position.z);
        time = 2 * Xoffset / speed;
        if (isMove)
        {
            PingPong(fromPos, toPos);
            animator.Play(Animator.StringToHash("Baddie-Run"));
        }
        else
        {
            animator.Play(Animator.StringToHash("Baddie-Idle"));

        }
    }

    private void PingPong(Vector3 from ,Vector3 to)
    {
        transform.DOMove(from, time).OnComplete(() => {
            PingPong(to, from);
        });
    }

    public void Dying()
    {

        source.Play();
        animator.Play(Animator.StringToHash("Baddie-Hurt"));
        transform.DOMoveY(transform.position.y - 50, 5).OnComplete(() =>
        {
            gameObject.SetActive(false);
        });
    }
  

}

